<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="100px">
    <!-- 需要隐藏的项上使用属性  v-show="showSearch" -->

      <el-form-item label="二级节点名称" prop="tagNodeName">
        <el-input
          style="width: 232px"
          v-model="queryParams.tagNodeName"
          maxlength="90"
          placeholder="请输入二级节点名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="二级节点前缀" prop="tagNodePrefix">
        <el-input
          style="width: 232px"
          v-model="queryParams.tagNodePrefix"
          maxlength="20"
          placeholder="请输入二级节点前缀"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="tagNodeStatus">
        <el-select v-model="queryParams.tagNodeStatus" placeholder="请选择状态" filterable clearable style="width: 232px">
          <el-option
            v-for="dict in dict.type.tag_node_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item class="search">
        <el-button type="primary" @click="handleQuery">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="table-content">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            icon="el-icon-circle-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['business:tagNode:add']"
          >新增</el-button>
        </el-col>
        <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
      </el-row>

      <el-table
        v-loading="loading"
        :data="tagNodeList"
        class="table-fixed"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column type="index" label="序号" width="55"  align="center" />
        <el-table-column label="二级节点id"  align="center" prop="id" v-if="false" show-overflow-tooltip/>
        <el-table-column label="二级节点名称"  align="center" prop="tagNodeName" show-overflow-tooltip/>
        <el-table-column label="二级节点前缀"  align="center" prop="tagNodePrefix" show-overflow-tooltip/>
        <el-table-column label="状态"  align="center" prop="tagNodeStatus" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.tagNodeStatus"
              active-value="1"
              inactive-value="2"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="changeStatus(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="创建时间"  align="center" prop="createTime" width="180" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160" fixed="right">
          <template slot-scope="scope">
            <!-- icon 详情：el-icon-view 修改：el-icon-edit 删除：el-icon-delete 审批、同意等: el-icon-s-check 新增：el-icon-plus 其他图标参考：https://element.eleme.cn/#/zh-CN/component/icon  -->
            <div style="text-align:right">
<!--                <el-button-->
<!--                v-hasPermi="['business:tagNode:view']"-->
<!--                size="mini"-->
<!--                icon="el-icon-view"-->
<!--                @click="handleUpdate(scope.row, 'view')"-->
<!--                >详情</el-button>-->
                <el-button
                v-hasPermi="['business:tagNode:edit']"
                size="mini"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row, 'edit')"
                >修改</el-button>
                <el-button
                v-hasPermi="['business:tagNode:remove']"
                size="mini"
                class="btn-delete"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                >删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 添加、查看、修改二级节点管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="140px" :disabled="openStatus === 'view'" label-position="top">
        <el-form-item label="二级节点名称" prop="tagNodeName">
          <el-input
            clearable
            v-model="form.tagNodeName"
            placeholder="请输入二级节点名称"
            maxlength="90"
            style="width:100%"
          />
        </el-form-item>
        <el-form-item label="二级节点前缀" prop="tagNodePrefix">
          <el-input
            clearable
            v-model="form.tagNodePrefix"
            placeholder="请输入二级节点前缀"
            maxlength="20"
            style="width:100%"
          />
        </el-form-item>
        <el-form-item label="超级管理员账号" prop="superUserName">
          <el-input
            clearable
            v-model="form.superUserName"
            placeholder="请输入超级管理员账号"
            maxlength="30"
            style="width:100%"
          />
        </el-form-item>
        <el-form-item label="超级管理员密码" prop="superUserPassword">
          <el-input
            clearable
            v-model="form.superUserPassword"
            placeholder="请输入超级管理员密码"
            maxlength="100"
            style="width:100%"
          />
        </el-form-item>
        <el-form-item label="二级节点对应的接口地址" prop="tagNodeUri">
          <el-input
            clearable
            v-model="form.tagNodeUri"
            placeholder="请输入二级节点对应的接口地址"
            maxlength="100"
            style="width:100%"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="openStatus !== 'view'" :loading="buttonLoading" type="primary" size="mini" @click="submitForm">确 定</el-button>
        <el-button :loading="buttonLoading" @click="cancel" size="mini">{{ openStatus === 'view' ? '关 闭' : '取 消' }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTagNode, getTagNode, delTagNode, addTagNode, updateTagNode, changeStatus } from "@/api/business/tagNode";

export default {
  name: "TagNode",
  dicts: ['tag_node_status'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 二级节点管理表格数据
      tagNodeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 显示弹状态 edit view
      openStatus: 'view',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tagNodeName: undefined,
        tagNodePrefix: undefined,
        tagNodeStatus: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        tagNodeName: [
          { required: true, message: "二级节点名称不能为空", trigger: "blur" }
        ],
        tagNodePrefix: [
          { required: true, message: "二级节点前缀不能为空", trigger: "blur" }
        ],
        superUserName: [
          { required: true, message: "超级管理员账号不能为空", trigger: "blur" }
        ],
        superUserPassword: [
          { required: true, message: "超级管理员密码不能为空", trigger: "blur" }
        ],
        tagNodeUri: [
          { required: true, message: "二级节点对应的接口地址不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询二级节点管理列表 */
    getList() {
      this.loading = true;
      listTagNode(this.queryParams).then(response => {
        this.tagNodeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        tagNodeName: undefined,
        tagNodePrefix: undefined,
        superUserName: undefined,
        superUserPassword: undefined,
        tagNodeUri: undefined,
        tagNodeStatus: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openStatus = 'add';
      this.open = true;
      this.title = "新增二级节点";
    },
    /** 修改按钮操作 */
    handleUpdate(row, type) {
      this.loading = true;
      this.openStatus = type;
      this.reset();
      const id = row.id || this.ids
      getTagNode(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = type === 'view' ? '查看二级节点管理' : '修改二级节点管理';
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateTagNode(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addTagNode(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除此二级节点？').then(() => {
        this.loading = true;
        return delTagNode(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/tagNode/export', {
        ...this.queryParams
      }, `tagNode_${new Date().getTime()}.xlsx`)
    },
    changeStatus(row) {
      let text = row.tagNodeStatus === "1" ? "重启" : "停用";
      this.$modal.confirm('是否确认' + text + '此二级节点？').then(() => {
        return changeStatus(row.id, row.tagNodeStatus);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.tagNodeStatus = row.tagNodeStatus === "1" ? "2" : "1";
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.table-fixed {
  ::v-deep .el-table__fixed-right {
    height: 100% !important; //设置高优先，以覆盖内联样式
  }
  ::v-deep .el-table__fixed {
    height: 100% !important; //设置高优先，以覆盖内联样式
  }
}
</style>
